package org.pidan.basic.designModel.agency.DongTai;

import org.pidan.basic.designModel.agency.CGLIB.UserDao;
import org.pidan.basic.designModel.agency.CGLIB.UserDaoImpl;

import java.lang.reflect.Proxy;

/**
 * {@code @description}: 动态代理使用代理方法
 * 缺点：必须是面向接口，目标业务类必须实现接口
 * 优点：不用关心代理类，只需要在运行阶段才指定代理哪一个对象
 * {@code @author}: huangyuxin
 * {@code @date}: 2023-11-16
 */
public class Client {

    public static void main(String[] args) {

        // 被代理对象
        UserDaoImpl daoImpl = new UserDaoImpl();
        InvocationHandlerImpl invocationHandler = new InvocationHandlerImpl(daoImpl);

        //类加载器
        ClassLoader classLoader = daoImpl.getClass().getClassLoader();
        Class<?>[] interfaces = daoImpl.getClass().getInterfaces();

        // 主要装载器、一组接口及调用处理动态代理实例
        UserDao newProxyInstance = (UserDao) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
        newProxyInstance.save();


    }
}
